const app = getApp();
import grace from "../../../utils/grace.js";

grace.page({
    /**
     * 页面的初始数据
     */
    data: {
        attendance_id: 0,
        school_name: '',
        class_id: 0,
        class_name: '',
        attendance_date: '',
        attendances: [],
        is_all_attendance: false,
        total: {}
    },
    onShow() {
        let that = this;
        let d = new Date();
        that.$data = {
            'school_name': wx.getStorageSync('school_name'),
            'class_id': wx.getStorageSync('class_id'),
            'class_name': wx.getStorageSync('class_name'),
            'attendance_date': d.getFullYear() + '年' + (d.getMonth() + 1) + "月" + d.getDate() + '日'
        }

        that.getList();

    },
    getList() {
        let that = this;

        wx.showLoading({
            title: '加载中...',
        })
        let url = app.globalData.api + '/api/v1.0/class/attendance/' + that.$data.class_id + '/all';
        that.$http.get(url, {token: wx.getStorageSync('token')}).then((res) => {
            if (res.data.code == 0) {
                that.$data = {
                    'attendances': res.data.data,
                    'total': res.data.total,
                    'attendance_id': res.data.total.attendance_id
                }
            } else if (res.data.code == 10001) {
                wx.navigateTo({
                    url: "/pages/common/authorize/index"
                })
            } else {
                that.$data.attendances = [];
            }
          wx.hideLoading();
        }).catch(err => {
            console.log(err.status, err.message);
        });
    },
    switchAttendance(e) {
        let that = this;
        let token = wx.getStorageSync('token');
        wx.showLoading({
            title: '加载中...',
        })
        let url = app.globalData.api + '/api/v1.0/attendance/' + e.currentTarget.dataset.id;
        if (e.currentTarget.dataset.is_attendance) {
            url += '/absent';
        } else {
            url += '/attend';
        }
        that.$http.put(url, {token: token}).then((res) => {
            if (res.data.code == 0) {
                wx.showToast({
                    title: '成功',
                    icon: 'success',
                    duration: 1000,
                    mask: true
                })
                // 更新attendances
                for (let i = 0; i < that.$data.attendances.length; i++) {
                    if (that.$data.attendances[i].id == e.currentTarget.dataset.id) {
                        that.$data.attendances[i].is_attendance = !that.$data.attendances[i].is_attendance;
                    }
                }

                // 更新统计数据
                if (e.currentTarget.dataset.is_attendance) {
                    that.$data.total.attend_total = that.$data.total.attend_total - 1;
                    that.$data.total.absent_total = that.$data.total.absent_total + 1;
                } else {
                    that.$data.total.attend_total = that.$data.total.attend_total + 1;
                    that.$data.total.absent_total = that.$data.total.absent_total - 1;
                }
                that.$data.total.rate_total = (that.$data.total.attend_total / that.$data.total.student_total * 100).toFixed(2);

            } else {
                wx.showToast({
                    title: '失败',
                    icon: 'fail',
                    duration: 1000,
                    mask: true
                })
            }
            wx.hideLoading();
        })
    },
    switchAllAttendance() {
        let that = this;
        if (that.$data.is_all_attendance) {
            that.$data.is_all_attendance = false;
        } else {
            that.$data.is_all_attendance = true;
        }

        wx.showLoading({
            title: '加载中...',
        })
        let url = app.globalData.api + '/api/v1.0/class/attendance/' + that.$data.class_id;
        if (that.$data.is_all_attendance) {
            url += '/attend';
        } else {
            url += '/absent';
        }
        that.$http.put(url, {token: wx.getStorageSync('token')}).then((res) => {
            if (res.data.code == 0) {
                wx.showToast({
                    title: '成功',
                    icon: 'success',
                    duration: 1000,
                    mask: true
                })
                that.onShow();
            } else {
                wx.showToast({
                    title: '失败',
                    icon: 'fail',
                    duration: 1000,
                    mask: true
                })
            }
            wx.hideLoading();
        })
    },
    submitBtnTap(e) {
        let that = this;
        wx.showModal({
            title: '您确认提交吗？',
            content: '',
            success: function (res) {
                if (res.confirm) {
                    wx.showLoading({
                        title: '加载中...',
                    })
                    let url = app.globalData.api + '/api/v1.0/attendance/approval/' + that.$data.attendance_id;
                    that.$http.put(url, {token: wx.getStorageSync('token')}).then((res) => {
                        if (res.data.code == 0) {
                            wx.showToast({
                                title: '成功',
                                icon: 'success',
                                duration: 1000,
                                mask: true
                            })
                        } else {
                            wx.showToast({
                                title: '失败',
                                icon: 'fail',
                                duration: 1000,
                                mask: true
                            })
                        }
                        wx.hideLoading();
                    }).catch(err => {
                        console.log(err.status, err.message);
                    });
                }
            }
        })
    }
})